package com.example.windweatherapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity {

    private ViewPager2 viewPager;
    private View indicator1, indicator2, indicator3;
    private Button startButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 检查是否首次启动
        SharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE);
        boolean isFirstRun = sp.getBoolean("first_run", true);
        
        // 如果不是首次启动，直接进入主页
        if (!isFirstRun) {
            startMainActivity();
            return;
        }
        
        setContentView(R.layout.activity_guide);
        
        // 初始化视图
        viewPager = findViewById(R.id.viewPager);
        indicator1 = findViewById(R.id.indicator1);
        indicator2 = findViewById(R.id.indicator2);
        indicator3 = findViewById(R.id.indicator3);
        startButton = findViewById(R.id.startButton);
        
        // 创建引导页数据
        List<GuidePageAdapter.GuidePageItem> guideItems = createGuideItems();
        
        // 设置适配器
        GuidePageAdapter adapter = new GuidePageAdapter(guideItems);
        viewPager.setAdapter(adapter);
        
        // 页面滑动监听器
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                updateIndicators(position);
                // 最后一页显示开始按钮
                if (position == guideItems.size() - 1) {
                    startButton.setVisibility(View.VISIBLE);
                } else {
                    startButton.setVisibility(View.GONE);
                }
            }
        });
        
        // 开始按钮点击事件
        startButton.setOnClickListener(v -> {
            // 标记已完成引导
            SharedPreferences.Editor editor = sp.edit();
            editor.putBoolean("first_run", false);
            editor.apply();
            
            // 跳转到主页
            startMainActivity();
        });
    }
    
    // 创建引导页内容
    private List<GuidePageAdapter.GuidePageItem> createGuideItems() {
        List<GuidePageAdapter.GuidePageItem> items = new ArrayList<>();
        
        // 使用现有的天气图标作为引导页图片
        items.add(new GuidePageAdapter.GuidePageItem(
                R.drawable.sunny, 
                "实时天气", 
                "随时掌握全国各地的实时天气情况，做好出行准备"));
        
        items.add(new GuidePageAdapter.GuidePageItem(
                R.drawable.cloudy, 
                "天气预报", 
                "提供24小时和未来7天的精准天气预报，天气变化早知道"));
        
        items.add(new GuidePageAdapter.GuidePageItem(
                R.drawable.rain, 
                "生活指数", 
                "提供舒适度、穿衣和运动指数等生活建议，让您的生活更加便利"));
        
        return items;
    }
    
    // 更新底部指示器
    private void updateIndicators(int position) {
        indicator1.setBackgroundResource(position == 0 ? 
                R.drawable.indicator_selected : R.drawable.indicator_unselected);
        indicator2.setBackgroundResource(position == 1 ? 
                R.drawable.indicator_selected : R.drawable.indicator_unselected);
        indicator3.setBackgroundResource(position == 2 ? 
                R.drawable.indicator_selected : R.drawable.indicator_unselected);
    }
    
    // 跳转到主页
    private void startMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
} 